package com.hd123.rumba.gwt.widget.client;

import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FocusWidget;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.hd123.rumba.gwt.widget.client.Toolbar.ToolButton;

public class Util {
  public static final int KEY_DROPDOWN = 114; // F3

  public static boolean isNavKey(char keyCode) {
    return keyCode == KeyboardListener.KEY_LEFT || keyCode == KeyboardListener.KEY_RIGHT
        || keyCode == KeyboardListener.KEY_UP || keyCode == KeyboardListener.KEY_DOWN
        || keyCode == KeyboardListener.KEY_ENTER || keyCode == KeyboardListener.KEY_TAB;
  }

  /**
   * IE6的bug: 当textBox中没有内容的时候, 显示输入焦点
   * 
   * @param textBox
   */
  public static void focusTextBox(TextBox textBox) {
    textBox.setFocus(true);
    if (textBox.getText() == null || textBox.getText().length() == 0)
      textBox.setCursorPos(0);
    else
      textBox.selectAll();
  }

  private static class ErrorDialog extends DialogBox implements ClickListener {
    private HTML body = new HTML("");

    public ErrorDialog() {
      setStylePrimaryName("jdgwt-ErrorDialog");
      Button closeButton = new Button("  关闭  ", this);
      VerticalPanel panel = new VerticalPanel();
      panel.setSpacing(4);
      panel.add(body);
      panel.add(new Label());
      panel.add(closeButton);
      panel.setCellHorizontalAlignment(closeButton, VerticalPanel.ALIGN_RIGHT);
      setWidget(panel);
      setWidth("100%");
    }

    public String getBody() {
      return body.getHTML();
    }

    public void onClick(Widget sender) {
      hide();
    }

    public void setBody(String html) {
      body.setHTML(html);
    }
  }

  public static ErrorDialog errorDialog = null;

  public static void showErrorDialog(String html) {
    if (errorDialog == null) {
      errorDialog = new ErrorDialog();
      errorDialog.setText("错误");
    }
    errorDialog.setBody(html);
    errorDialog.show();
    errorDialog.center();
  }

  public static class ProgressDialog extends DialogBox {
    private HTML body = new HTML("");

    public ProgressDialog() {
      super();
      setStyleName("jdgwt-ProgressDialog");
      VerticalPanel panel = new VerticalPanel();
      panel.setSpacing(4);
      panel.add(body);
      panel.add(new Label());
      setWidget(panel);
      setWidth("100%");
    }

    public String getBody() {
      return body.getHTML();
    }

    public void setBody(String html) {
      body.setHTML(html);
    }
  }

  public static ProgressDialog progressDialog = null;

  public static void showProgressDialog(String html) {
    if (progressDialog == null) {
      progressDialog = new ProgressDialog();
      progressDialog.setText("请稍候...");
    }
    progressDialog.setBody(html);
    progressDialog.body.setWidth("100%");
    progressDialog.show();
    progressDialog.center();
  }

  public static void hideProgressDialog() {
    if (progressDialog != null)
      progressDialog.hide();
  }

  public static Grid progressInfo = null;

  /**
   * 在右上角显示异步调用等待消息的提示.
   * 
   * @param text
   */
  public static void showProgressInfo(String text) {
    if (progressInfo == null) {
      progressInfo = new Grid(1, 1);
      progressInfo.getCellFormatter().setWordWrap(0, 0, false);
      progressInfo.setStyleName("jdgwt-ProgressText");
    }
    RootPanel.get().add(progressInfo, 0, 0);
    progressInfo.setVisible(true);
    progressInfo.setText(0, 0, text);
    DOM.setStyleAttribute(progressInfo.getElement(), "left",
        (Window.getClientWidth() - progressInfo.getOffsetWidth()) + "px");
    DOM.setStyleAttribute(progressInfo.getElement(), "top", (Window.getScrollTop() + 1) + "px");
  }

  public static void hideProgressInfo() {
    if (progressInfo != null)
      progressInfo.setVisible(false);
  }

  public static class EnterToTabListener implements KeyboardListener {
    public void onKeyDown(Widget sender, char keyCode, int modifiers) {
      handleKey(sender, keyCode, modifiers);
    }

    public void onKeyPress(Widget sender, char keyCode, int modifiers) {
      handleKey(sender, keyCode, modifiers);
    }

    public void onKeyUp(Widget sender, char keyCode, int modifiers) {
      handleKey(sender, keyCode, modifiers);
    }

    private void handleKey(Widget sender, char keyCode, int modifiers) {
      if (keyCode == KEY_ENTER) {
        if (sender instanceof FocusWidget) {
          // ((FocusWidget) sender).setKey((char) KEY_TAB);
        }
      }
    }
  }

  private static EnterToTabListener enterToTabListener = null;

  public static KeyboardListener getEnterToTabListener() {
    if (enterToTabListener == null)
      enterToTabListener = new EnterToTabListener();
    return enterToTabListener;
  }

  /**
   * 在ListBox中查找特定值并选中之，如果没有找到会选中第0个
   * 
   * @param listBox
   * @param value
   *            要查找的值
   */
  public static void selectListBoxByValue(ListBox listBox, String value) {
    if (listBox == null)
      return;

    for (int i = 0; i < listBox.getItemCount(); i++)
      if (listBox.getValue(i).equals(value)) {
        listBox.setSelectedIndex(i);
        return;
      }

    if (listBox.getItemCount() != 0)
      listBox.setSelectedIndex(0);
  }

  /** 隐藏工具栏中可能出现的重复的分隔符 */
  public static void trimToolbar(Toolbar toolbar) {
    HorizontalPanel hp = toolbar.inner;

    for (int i = 0; i < hp.getWidgetCount(); i++) {
      if (isSplitor((ToolButton) hp.getWidget(i))) {
        int ii = i;
        ToolButton prior = null, next = null;

        // get prior visible widget
        while (--ii >= 0) {
          if (hp.getWidget(ii).isVisible()) {
            prior = (ToolButton) hp.getWidget(ii);
            break;
          }
        }

        // get next visible widget
        ii = i;
        while (++ii < hp.getWidgetCount()) {
          if (hp.getWidget(ii).isVisible()) {
            next = (ToolButton) hp.getWidget(ii);
            break;
          }
        }

        hp.getWidget(i).setVisible(
            prior != null && next != null && !isSplitor(prior) && !isSplitor(next));
      }

    }

  }

  /** 判断ToolButton是否为分隔符 */
  private static boolean isSplitor(ToolButton button) {
    return button != null && button.getHTML().equals("|");
  }

}
